﻿using Ldt.DataEngine.DataFlowParse.Model;
using LdtDataEngin.ModuleDetail.CommonModule;
using LdtDataEngin.ModuleDetail.CommonModule.PersonMerger;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.Collection;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace LdtDataEngin.Moudle
{
    /// <summary>
    /// 数据推导服务模块
    /// </summary>
    public class MoudleDataDeduce : MoudleBase
    {
        private IMoudle _moudle;
        private static List<RequestDataModel> requests;
        /// <summary>
        /// 数据推导服务模块
        /// </summary>
        /// <param name="moudle"></param>
        public MoudleDataDeduce(IMoudle moudle)
        {
            _moudle = moudle;
        }


        /// <summary>
        /// 执行模块
        /// </summary>
        /// <param name="dataContext"></param>
        public override void MoudleHandle(DataContext dataContext)
        {

            //执行当前模块的相关业务逻辑
            string moudleName = typeof(MoudleDataDeduce).FullName;
            moudleName += "_" + this.GetHashCode();
            // 设置模块进度
            dataContext.InteractiveShow.ModuleProcessShow?.Invoke(moudleName, 0, dataContext.InteractiveShow.inputParameterModel);
            // 创建计时器
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            try
            {
                dataContext.InteractiveShow.LogShow("***************************" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--->开始执行数据推导模块*****************************");
                DataDeduceDetail dataDeduce = new DataDeduceDetail(dataContext, moudleName);
                dataDeduce.Extrat();
                ConfigModel moudelConfig = dataContext.DataLayerSys.GetConfigModel(moudleName);
                ConfigModel dataDeduceConfig = moudelConfig.ConfigDetail?.FirstOrDefault(x => x.Code.ToLower() == "qxqjmerge");
                ConfigModel plotExcluConfig = moudelConfig.ConfigDetail?.FirstOrDefault(x => x.Code.ToLower() == "commonplotexclusion");
                if (dataDeduceConfig != null)
                {
                    if (!string.IsNullOrEmpty(dataDeduceConfig.ConfigContent))
                    {
                        SituationPlotData situationPlotData = new SituationPlotData() { dataContext = dataContext };
                        situationPlotData.PreferredMerge(dataDeduceConfig.ConfigContent, plotExcluConfig?.ConfigContent);
                    }
                }
            }
            catch (Exception ex)
            {
                dataContext.InteractiveShow.LogShow("数据推导模块执行异常！错误消息为：" + ex.Message, LogMessageType.Error);
                dataContext.InteractiveShow.LogShow(ex.StackTrace, LogMessageType.Error);
            }
            dataContext.InteractiveShow.LogShow("***************************" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--->数据推导模块执行完成*****************************");
            // 设置模块进度
            stopwatch.Stop();
            requests = DataDeduceDetail.DeduceRequestModels;
            dataContext.InteractiveShow.ModuleProcessShow?.Invoke(moudleName + "_" + stopwatch.Elapsed.TotalMilliseconds.ToString("0"), 100, dataContext.InteractiveShow.inputParameterModel);
            _moudle.MoudleHandle(dataContext);
        }

        /// <summary>
        /// 模块处理结果
        /// </summary>
        /// <returns></returns>
        public static HandleResultModel MoudleHandleResult()
        {
            HandleResultModel resultModel = new HandleResultModel();
            if(requests != null)
            {
                resultModel.ResultType = "3";
                resultModel.ResultData = requests;
            }
            return resultModel;
        }
    }
}
